package com.yeban.javase.day12.test.streamtest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/22
 *
 * 6.获取员工工资最高的人
 */
public class StreamTest06 {

    public static void main(String[] args) {
        List<Employee1> employees = Arrays.asList (
                new Employee1 ("张三", 7500.0),
                new Employee1 ("李四", 8200.0),
                new Employee1 ("王五", 9000.0),
                new Employee1 ("赵六", 7800.0),
                new Employee1 ("孙七", 10000.0));

        // 方法一  max
        employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).ifPresent(System.out::println);
        // 方法引用
        employees.stream().max(Comparator.comparingDouble(Employee1::getSalary)).ifPresent(System.out::println);

        // 方法二  归约
        // 只能获取薪资
        employees.stream().map(Employee1::getSalary).reduce(Double::max).ifPresent(System.out::println);
        // 获取薪资高的那个对象
        employees.stream().reduce((e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2)
                .ifPresent(System.out::println);

        // 方法三 重写Employee1中的比较器方法，naturalOrder
        employees.stream().max(Comparator.naturalOrder()).ifPresent(System.out::println);
    }
}

class Employee1 implements Comparable<Employee1> {
    private String name;
    private double salary;

    public Employee1() {
    }

    public Employee1(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Employee1 o) {
        return Double.compare(this.salary,o.salary);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String toString() {
        return "Employee1{name = " + name + ", salary = " + salary + "}";
    }
}